<template>
  <div>
    <el-button @click="show=true">+选择分类</el-button>

    <!-- 弹出框 -->
    <el-dialog title="选择分类" :visible.sync="show">
      <el-tree
        ref="tree"
        :data="cateData"
        show-checkbox
      ></el-tree>

      <span slot="footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      show: false,
      cateData: []
    }
  },
  created() {
    axios.get('/course/classify').then(res=>{
      if(res) {
        this.cateData = res.data
      }
    })
  },
  methods: {
    submit() {
      // 获取勾选的内容
      const _nodes = this.$refs.tree.getCheckedNodes()

      // 取出所有数组的 id 和 name ，其他不要
      let nodes = []
      _nodes.forEach(v=>{
        nodes.push({
          id: v.id,
          name: v.name
        })
      })

      // 返回新数组
      this.$emit('ok', nodes)

      this.show = false
    }
  }
}
</script>
